%% Consolidation scenario
% by IMF Institute for Capacity Development - Course on Macro-fiscal modeling and analysis

close all;
clear all;

%%%%%
%% READ THE MODEL AND PREPARE THE DATA
%%%%%

%% Read the model
readmodel;

%% Data range for the simulation
startsim = yy(2011);
endsim = yy(2030);
rng = startsim:endsim;

%% Create a database filled with 'Steady-state' values
d = sstatedb(m,rng(1));

%% Set the debt trajectory (this must be done manually)
d.debt_tar_rat(yy(2011)) = 28;
d.debt_tar_rat(yy(2012)) = 26;
d.debt_tar_rat(yy(2013)) = 24;
d.debt_tar_rat(yy(2014)) = 23;
d.debt_tar_rat(yy(2015)) = 22;
d.debt_tar_rat(yy(2016)) = 21;
d.debt_tar_rat(yy(2017):yy(2030)) = 20;

%% Add shocks to the database
% Negative (decline) shock to foreign output gap
%d.shock_l_y_eu_gap(yy(2012)) = -2;
%d.shock_l_y_eu_gap(yy(2013)) = -1;

%%%%%
%% SIMULATE THE SCENARIO
%%%%%

%% Define the simulation plan
simplan = plan(m,rng);

%% Fix the debt target
simplan = exogenize(simplan,{'debt_tar_rat'},rng);
simplan = endogenize(simplan,{'shock_debt_tar'},rng);

%% Simulate the scenario
s = simulate(m,d,rng,'plan',simplan);

%% Merge historical and simulated data
s = dbextend(d,s);

% Results are saved in file 'ss_fdata.csv'  
dbsave('ss_fdata.csv',s);

%%%%%
%% Make the report
%%%%%

x = report.new('Scenario_ss','visible',true);

% Figures
sty = struct();
sty.line.linewidth = 1.5;
sty.line.linestyle = {'-';'--'};
sty.line.color = {'k';'r'};
sty.axes.box = 'on';
sty.legend.location = 'Best';


x.figure('"Out of Steady-state" Scenario 2011 - 2030','subplot',[3,3],'style',sty,'range',startsim-1:endsim,'dateformat','YYYY:P');

x.graph([s.debt_rat.comment ' and the Target'],'legend',false);
x.series('',[s.debt_rat s.debt_tar_rat]);
x.highlight('',startsim-1:startsim-1);

x.graph([s.def_rat.comment],'legend',false);
x.series('',[s.def_rat]);
x.highlight('',startsim-1:startsim-1);

x.graph(s.def_struct_rat.comment,'legend',false);
x.series('',[s.def_struct_rat]);
x.highlight('',startsim-1:startsim-1);

x.graph(s.fiscimp_rat.comment,'legend',false);
x.series('',[s.fiscimp_rat]);
x.highlight('',startsim-1:startsim-1);

x.graph(s.l_y_gap.comment,'legend',false);
x.series('',[s.l_y_gap]);
x.highlight('',startsim-1:startsim-1);

x.graph(s.dl_y.comment,'legend',false);
x.series('',[s.dl_y]);
x.highlight('',startsim-1:startsim-1);

x.graph(s.dl_cpi.comment,'legend',false);
x.series('',[s.dl_cpi]);
x.highlight('',startsim-1:startsim-1);

x.graph('Monetary Policy Rate (1Y)','legend',false);
x.series('',[s.rsg1]);
x.highlight('',startsim-1:startsim-1);

x.graph(s.dl_czk_eur.comment,'legend',false);
x.series('',[s.dl_czk_eur]);
x.highlight('',startsim-1:startsim-1);

x.pagebreak();

x.figure('"Out of Steady-state" Scenario 2011 - 2030','subplot',[3,3],'style',sty,'range',startsim-1:endsim,'dateformat','YYYY:P');

x.graph(s.rsg3.comment,'legend',false);
x.series('',[s.rsg3]);
x.highlight('',startsim-1:startsim-1);

x.graph(s.rsg10.comment,'legend',false);
x.series('',[s.rsg10]);
x.highlight('',startsim-1:startsim-1);

x.graph(s.rsg_fcy.comment,'legend',false);
x.series('',[s.rsg_fcy]);
x.highlight('',startsim-1:startsim-1);

x.graph(s.prem.comment,'legend',false);
x.series('',[s.prem]);
x.highlight('',startsim-1:startsim-1);

x.graph(s.tprem3.comment,'legend',false);
x.series('',[s.tprem3]);
x.highlight('',startsim-1:startsim-1);

x.graph(s.tprem10.comment,'legend',false);
x.series('',[s.tprem10]);
x.highlight('',startsim-1:startsim-1);

x.graph(s.lrr_tnd.comment,'legend',false);
x.series('',[s.lrr_tnd]);
x.highlight('',startsim-1:startsim-1);

x.graph([s.l_y_tnd.comment 'y-o-y'],'legend',false);
x.series('',[s.l_y_tnd - s.l_y_tnd{-1}]);
x.highlight('',startsim-1:startsim-1);

x.graph([s.l_z_tnd.comment 'y-o-y'],'legend',false);
x.series('',[s.l_z_tnd - s.l_z_tnd{-1}]);
x.highlight('',startsim-1:startsim-1);

x.publish('Scenario_ss','display',false);